<template>
  <div class="min-h-full">
    <nav class="bg-gray-800">
      <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="flex items-center justify-between h-16">
          <div class="flex items-center">
            <div class="flex-shrink-0">
              <img :src="logo" class="h-8 w-8" alt="logo"/>
            </div>
            <h2 class="text-2xl text-white ml-2">{{ title }}</h2>
            <div class="hidden md:block">
              <div class="ml-10 flex items-baseline space-x-4">
                <!-- Current: "bg-gray-900 text-white", Default: "text-gray-300 hover:bg-gray-700 hover:text-white" -->
                <!-- PC端菜单 -->
                <a href="#"
                   @click.prevent="to({name: 'Dashboard'})"
                   :class="active('Dashboard')">仪表盘</a>
                <a href="#"
                   @click.prevent="to({name: 'Spending'})"
                   :class="active('Spending')">我的支出</a>
              </div>
            </div>
          </div>
          <div class="hidden md:block">
            <div class="ml-4 flex items-center md:ml-6">
              <button type="button"
                      class="bg-gray-800 p-1 rounded-full text-gray-400 hover:text-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-white">
                <span class="sr-only">View notifications</span>
                <!-- Heroicon name: outline/bell -->
                <svg class="h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2"
                     stroke="currentColor" aria-hidden="true">
                  <path stroke-linecap="round" stroke-linejoin="round"
                        d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9"/>
                </svg>
              </button>

              <!-- Profile dropdown -->
              <div class="ml-3 relative">
                <div @click="profileDropdownIsOpen = !profileDropdownIsOpen">
                  <button type="button"
                          class="max-w-xs bg-gray-800 rounded-full flex items-center text-sm focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-white"
                          id="user-menu-button" aria-expanded="false" aria-haspopup="true">
                    <span class="sr-only">Open user menu</span>
                    <img class="h-8 w-8 rounded-full"
                         src="https://q1.qlogo.cn/g?b=qq&nk=77266837&s=100"
                         alt="user">
                  </button>
                </div>
                <!-- PC端头像下滑菜单 -->
                <transition name="el-zoom-in-top" mode="out-in">
                  <div class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg py-1 bg-white ring-1
                ring-black ring-opacity-5 focus:outline-none"
                       v-if="profileDropdownIsOpen"
                       role="menu" aria-orientation="vertical" aria-labelledby="user-menu-button" tabindex="-1">

                    <!-- Active: "bg-gray-100", Not Active: "" -->
                    <a href="#" class="block px-4 py-2 text-sm text-gray-700" role="menuitem" tabindex="-1"
                       id="user-menu-item-0">我的资料</a>

                    <a @click.prevent="$router.push({'name': 'UpdatePassword'})"
                       href="#"
                       class="block px-4 py-2 text-sm text-gray-700" role="menuitem" tabindex="-1"
                       id="user-menu-item-1">修改密码</a>

                    <a href="#" @click.prevent="logout()" class="block px-4 py-2 text-sm text-gray-700" role="menuitem"
                       tabindex="-1"
                       id="user-menu-item-2">注销登陆</a>

                  </div>
                </transition>
              </div>
            </div>
          </div>
          <div class="-mr-2 flex md:hidden">
            <!-- Mobile menu button -->
            <button type="button" @click="menuDropdownIsOpen = !menuDropdownIsOpen"
                    class="bg-gray-800 inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-white hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-white"
                    aria-controls="mobile-menu" aria-expanded="false">
              <span class="sr-only">Open main menu</span>
              <!--
                Heroicon name: outline/menu

                Menu open: "hidden", Menu closed: "block"
              -->
              <svg class="block h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
                   stroke-width="2" stroke="currentColor" aria-hidden="true">
                <path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h16M4 18h16"/>
              </svg>
              <!--
                Heroicon name: outline/x

                Menu open: "block", Menu closed: "hidden"
              -->
              <svg class="hidden h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
                   stroke-width="2" stroke="currentColor" aria-hidden="true">
                <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"/>
              </svg>
            </button>
          </div>
        </div>
      </div>

      <!-- Mobile menu, show/hide based on menu state. -->
      <transition name="el-zoom-in-top" mode="out-in">
        <div v-if="menuDropdownIsOpen" class="md:hidden" id="mobile-menu">
          <div class="px-2 pt-2 pb-3 space-y-1 sm:px-3">
            <!-- Current: "bg-gray-900 text-white", Default: "text-gray-300 hover:bg-gray-700 hover:text-white" -->
            <!-- 移动端菜单 -->
            <a href="#"
               @click.prevent="to({name: 'Dashboard'})"
               :class="activeMobile('Dashboard')">仪表盘</a>
            <a href="#"
               @click.prevent="to({name: 'Spending'})"
               :class="activeMobile('Spending')">支出列表</a>
          </div>
          <div class="pt-4 pb-3 border-t border-gray-700">
            <div class="flex items-center px-5">
              <div class="flex-shrink-0">
                <img class="h-10 w-10 rounded-full"
                     src="https://q1.qlogo.cn/g?b=qq&nk=77266837&s=100"
                     alt="">
              </div>
              <div class="ml-3">
                <div class="text-base font-medium leading-none text-white">{{ userInfo.username }}</div>
              </div>
              <button type="button"
                      class="ml-auto bg-gray-800 flex-shrink-0 p-1 rounded-full text-gray-400 hover:text-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-white">
                <span class="sr-only">View notifications</span>
                <!-- Heroicon name: outline/bell -->
                <svg class="h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2"
                     stroke="currentColor" aria-hidden="true">
                  <path stroke-linecap="round" stroke-linejoin="round"
                        d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9"/>
                </svg>
              </button>
            </div>
            <!-- 移动端头像下滑菜单 -->
            <div class="mt-3 px-2 space-y-1">
              <a href="#"
                 class="block px-3 py-2 rounded-md text-base font-medium text-gray-400 hover:text-white hover:bg-gray-700">
                我的资料
              </a>

              <a @click.prevent="$router.push({'name': 'UpdatePassword'})"
                 href="#"
                 class="block px-3 py-2 rounded-md text-base font-medium text-gray-400 hover:text-white hover:bg-gray-700">
                修改密码
              </a>

              <a href="#" @click.prevent="logout()"
                 class="block px-3 py-2 rounded-md text-base font-medium text-gray-400 hover:text-white hover:bg-gray-700">
                注销登录
              </a>
            </div>
          </div>
        </div>
      </transition>
    </nav>

    <router-view v-slot="{Component}">
      <!--  路由页面之间切换的过度动画  -->
      <transition name="el-fade-in-linear" mode="out-in">
        <Component :is="Component"></Component>
      </transition>
    </router-view>
  </div>
</template>

<script>

import {mapState} from 'vuex'
import {ElMessage, ElMessageBox} from 'element-plus'
import logo from '@/assets/img/money.svg'
import request from "@/http/request";

export default {
  name: 'HomeLayout',
  computed: mapState(['userInfo', 'title']),
  data() {
    return {
      profileDropdownIsOpen: false,
      menuDropdownIsOpen: false,
      logo: logo
    }
  },
  methods: {
    to(r) {
      if (this.menuDropdownIsOpen === true) {
        setTimeout(() => {
          this.menuDropdownIsOpen = false
          this.$router.push(r)
        }, 100)
      } else {
        this.$router.push(r)
      }
    },
    active(name) {
      let css = ''
      if (this.$route.name === name) {
        css = 'bg-gray-900 text-white px-3 py-2 rounded-md text-sm font-medium'
      } else {
        css = 'text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium'
      }
      return css
    },
    activeMobile(name) {
      let css = ''
      if (this.$route.name === name) {
        css = 'bg-gray-900 text-white block px-3 py-2 rounded-md text-base font-medium'
      } else {
        css = 'text-gray-300 hover:bg-gray-700 hover:text-white block px-3 py-2 rounded-md text-base font-medium'
      }
      return css
    },
    logout() {
      ElMessageBox.confirm('确定要注销登录吗?', '注销登录', {
        type: 'warning',
        cancelButtonText: '算了',
        confirmButtonText: '确定'
      }).then(() => {
        request.logout().then(res => {
          if (res.data.status === 200) {
            this.$store.commit('token', null)
            ElMessage.success('注销登录成功，即将跳转到登陆页...')
            setTimeout(() => {
              this.$router.push({name: 'Login'})
            }, 800)
          } else {
            ElMessage.error(res.data.message)
          }
        })
      })
    },
  }
}
</script>

<style scoped>

</style>